//
//  ShopTableView.m
//  Restaurant
//
//  Created by wanghuan on 15/4/22.
//  Copyright (c) 2015年 wanghuan. All rights reserved.
//

#import "ShopTableView.h"

@implementation ShopTableView {

    UIAlertView *alterView;

}

/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/

- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        // Initialization code
        self.delegate = self;
        self.dataSource = self;
        
        self.backgroundColor = AppBackGroundColor;
        
        _DeleteArray = [[NSMutableArray alloc]init];
        
        _cellArray = [[NSMutableArray alloc]init];
        
    }
    return self;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    
    if (self.shopBtnSelected.selected) {
        
      
        if (_shopArr.count > 2) {
            self.frame = CGRectMake(0, APPHeight-50*3-40-49, APPWith,50*3+40);
        } else {
            
            self.frame = CGRectMake(0, APPHeight-50*_shopArr.count-40-49, APPWith,50*_shopArr.count+40);
            
        }
    }
    return self.shopArr.count;
    
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {

    
    return 1;

}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {

    return 49;

}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {

    ShoppingTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"shop"];
    
    if (!cell) {
        cell = [[NSBundle mainBundle]loadNibNamed:@"ShoppingTableViewCell" owner:self options:nil].lastObject;
    }
    
    shoppingModel *model = [_shopArr objectAtIndex:indexPath.row];
    
    cell.nameLabel.text = model.nameStr;
    
    cell.priceLabel.text = model.prices;
    
    cell.numberLabel.text = model.number;
    
    cell.isEditing = NO;
    
    cell.indexPath = indexPath;
    
//    cell.backgroundColor = [UIColor whiteColor];
    
//    UITapGestureRecognizer *recognize = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(recognizeClick:)];
//
//    [cell.contentView addGestureRecognizer:recognize];
//    recognize.view.tag = indexPath.row;
//    NSLog(@"%ld,%ld",(long)recognize.view.tag,(long)indexPath.row);
    
    [cell.addBtn addTarget:self action:@selector(addClick:) forControlEvents:UIControlEventTouchUpInside];
    [cell.deleteBtn addTarget:self action:@selector(deleteClick:) forControlEvents:UIControlEventTouchUpInside];
    
    cell.addBtn.tag = model.tag;
    cell.deleteBtn.tag = model.tag;
    
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    
    [_cellArray addObject:cell];
    
    return cell;


}

- (void)recognizeClick:(UITapGestureRecognizer *)recognize {

    
    ShoppingTableViewCell *cell = [_cellArray objectAtIndex:recognize.view.tag];
    
    //取消cell点击事件的类型
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    
    cell.isEditing = !cell.isEditing;
    
    if (cell.isEditing) {
        
        
        cell.backgroundColor = [UIColor grayColor];
        [self selectRowAtIndexPath:cell.indexPath animated:YES scrollPosition:UITableViewScrollPositionNone];
        
         [_DeleteArray addObject:[_shopArr objectAtIndex:cell.indexPath.row]];
    } else {
    
       
        [_DeleteArray removeObject:[_shopArr objectAtIndex:cell.indexPath.row]];
//        cell.backgroundColor = [UIColor whiteColor];
        [self deselectRowAtIndexPath:cell.indexPath animated:YES];
        
    }


}




- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {

    return 40;

}

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {

    
    
    UIView *view = [[UIView alloc]initWithFrame:CGRectMake(0, 0, APPWith, 40)];
    
    view.backgroundColor = [UIColor redColor];
    
    NSArray *arr = @[@"shopping1.png",@"删除"];
    
    for (int i = 0; i < 2; i++) {
        UIButton *shopBtn = [[UIButton alloc]initWithFrame:CGRectMake(fabs(10-i*(APPWith-60)), 5, 34, 34)];
        
        [shopBtn setBackgroundImage:[UIImage imageNamed:arr[i]] forState:UIControlStateNormal];
        
        shopBtn.tag = i+1;
        
        [shopBtn addTarget:self action:@selector(shopClick:) forControlEvents:UIControlEventTouchUpInside];
        
        if (i) {
            [shopBtn setTitle:arr[i] forState:UIControlStateNormal];
        }
        
        shopBtn.titleLabel.font = [UIFont systemFontOfSize:13];
        [view addSubview:shopBtn];
    }
    
    return view;
    

}


#pragma mark - 添加和删除
- (void)addClick:(UIButton *)sender {

    self.AddBlock(sender);

}

- (void)deleteClick:(UIButton *)sender {

    self.DelegateBlock(sender);
}

- (void)shopClick:(UIButton *)sender {

    if (sender.tag == 1) {
        
        self.shopBtnSelected.selected = !self.shopBtnSelected.selected;
        
        [UIView animateWithDuration:1 animations:^{
            self.shopBtnSelected.alpha = 1.0;
        }];
        
        
        
        if (self) {
            [UIView animateWithDuration:1 animations:^{
                self.frame = CGRectMake(0, APPHeight-49, APPWith, 3*50+40);
            
            }];
        }
        
        
        
    } else {
    
//        [_shopArr removeObjectsInArray:_DeleteArray];
//        
//        for (ShoppingTableViewCell *cell in _cellArray) {
//            cell.backgroundColor = [UIColor whiteColor];
//        }

        
        alterView = [[UIAlertView alloc]initWithTitle:@"提示" message:@"是否确定清空" delegate:self cancelButtonTitle:@"确定" otherButtonTitles:@"取消", nil];
            
        [alterView show];
        
        if (_shopArr.count > 2) {
            self.frame = CGRectMake(0, APPHeight-3*50-40-49, APPWith,3*50+40);
        } else {
            
            self.frame = CGRectMake(0, APPHeight-_shopArr.count*50-40-49, APPWith,_shopArr.count*50+40);
            
        }
        
        
    
    }

   

}

#pragma mark - UIAlertViewDelegate 

- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex {
    
    if (!buttonIndex) {
        
        if (_DeleteArray) {
            [_DeleteArray removeAllObjects];
        }
        
        [_DeleteArray addObjectsFromArray:_shopArr];
        
        [_shopArr removeAllObjects];
        
        [self reloadData];
        
        [[NSNotificationCenter defaultCenter]postNotificationName:@"ShopframeChange" object:nil];
    }


   

}


@end
